﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using TheDrinksApp.Screen;
using System.Collections.ObjectModel;
using TheDrinksApp.Models;
using TheDrinksApp.Database;
using TheDrinksApp.Screen.CurrentOrder;
using TheDrinksApp.Screen.RecentOrder;
using TheDrinksApp.Screen.Order;
using TheDrinksApp.Screen.RecentOrder.TableViewCell;

namespace TheDrinksApp
{
    public partial class MainPage : PhoneApplicationPage
    {
        private List<UserControl> pages = new List<UserControl>();
        private Stack<PAGE> backHistoryHome = new Stack<PAGE>();
        private Stack<PAGE> backHistoryRecentOrder = new Stack<PAGE>();
        private Stack<PAGE> backHistoryCurrentOrder = new Stack<PAGE>();
        private PAGE currentRootPage;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            GlobalVariable.mainPage = this;
            this.SetCurrentNumber();
            //Add all pages  to list
            Pages.Add(new HomeScreen(this));
            Pages.Add(new InfoScreen());
            Pages.Add(new ChooseCategoryScreen(this));
            Pages.Add(new CurrentOrderScreen(this));
            Pages.Add(new RecentOrderScreen(this));
            Pages.Add(new OrderItemDetail(this));
            Pages.Add(new SumaryScreen(this));
            Pages.Add(new SearchScreen(this));
            Pages.Add(new GlassSizeScreen(this));
            Pages.Add(new OrderScreen(this));
            Pages.Add(new ViewMixerScreen(this));
            pages.Add(new TheDrinksApp.Screen.CurrentOrder.HowMuchScreen( ));

            //Init root pages
            backHistoryHome.Push(PAGE.HOME);
            backHistoryRecentOrder.Push(PAGE.RECENT_ORDER);
            backHistoryCurrentOrder.Push(PAGE.CURRENT_ORDER);
            setCurrentRootPage(PAGE.HOME);
            this.BackKeyPress += MainPage_BackKeyPress;
        }

        void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (currentRootPage == PAGE.HOME && backHistoryHome.Count > 1)
            {
                this.ShowPrevPage();
                e.Cancel = true;
            }
            else if (currentRootPage == PAGE.CURRENT_ORDER && backHistoryCurrentOrder.Count > 1)
            {
                this.ShowPrevPage();
                e.Cancel = true;
            }
            else if (currentRootPage == PAGE.RECENT_ORDER && backHistoryRecentOrder.Count > 1)
            {
                this.ShowPrevPage();
                e.Cancel = true;
            }
            else{
                if (MessageBox.Show("Do you want to exit?", "Are you sure?", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                {
                    e.Cancel = true;
                }
            }
        }

        public List<UserControl> Pages
        {
            get { return pages; }
            set { pages = value; }
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            ShowPage(PAGE.HOME);
        }
        public void SetCurrentNumber()
        {
            int total = 0 ;
            foreach (CurrentOrderDAO order in Util.currentOrders)
            {
                total += order.quantity;
            }
            if (Util.currentOrders.Count == 0)
            {
                orderNumberBottom.Visibility = Visibility.Collapsed;
                numberOrderBottom.Visibility = Visibility.Collapsed;
                curentOrder.Text = numberOrderBottom.Text = total.ToString();
            }
            else
            {
                curentOrder.Text = numberOrderBottom.Text = total.ToString();
                numberOrderBottom.Foreground = new SolidColorBrush(Colors.White);
                orderNumberBottom.Visibility = Visibility.Visible;
                numberOrderBottom.Visibility = Visibility.Visible;
            }
        }
        #region Page Manager
        public void setCurrentRootPage(PAGE page)
        {
            
            if (page == PAGE.HOME)
            {
                btnHome.IsChecked = true;
                btnRecentOrder.IsChecked = false;
                btnCurrentOrder.IsChecked = false;
                currentRootPage = page;
            }
            else if (page == PAGE.CHOOSE_CATEGORY)
            {
                btnHome.IsChecked = true;
                btnRecentOrder.IsChecked = false;
                btnCurrentOrder.IsChecked = false;
            }
            else if (page == PAGE.RECENT_ORDER)
            {
                btnHome.IsChecked = false;
                btnRecentOrder.IsChecked = true;
                btnCurrentOrder.IsChecked = false;
                currentRootPage = page;
            }
            else if (page == PAGE.CURRENT_ORDER)
            {
                btnHome.IsChecked = false;
                btnRecentOrder.IsChecked = false;
                btnCurrentOrder.IsChecked = true;
                currentRootPage = page;
            }
        }
        
        public void showButton(BUTTON_VISIBILITY which)
        {
            if (which == BUTTON_VISIBILITY.BACK_RIGHT)
            {
                btnBack.Visibility = Visibility.Visible;
                btnInfo.Visibility = Visibility.Collapsed;
                dummyButton.Visibility = Visibility.Collapsed;
            }
            else if (which == BUTTON_VISIBILITY.INFO_RIGHT)
            {
                btnBack.Visibility = Visibility.Collapsed;
                btnInfo.Visibility = Visibility.Visible;
                dummyButton.Visibility = Visibility.Collapsed;
            }
            else if (which == BUTTON_VISIBILITY.NONE_RIGHT)
            {
                btnBack.Visibility = Visibility.Collapsed;
                btnInfo.Visibility = Visibility.Collapsed;
                dummyButton.Visibility = Visibility.Visible;
            }
        }

        public void ShowPage(PAGE page)
        {
            //change fullscreen for how much screen
            ChangeFullScreenNumpadPage(page);
            SetCurrentNumber();
            if (page == PAGE.HOW_MUCH_CURRENT )
            {
                if (page == PAGE.HOW_MUCH_CURRENT)
                {
                    pageTransitionControl.ShowPage(Pages[(int)PAGE.HOW_MUCH_CURRENT]);
                }

                if (currentRootPage == PAGE.HOME)
                {
                    if (backHistoryHome.Count >= 2)
                    {
                        backHistoryHome.Pop();
                    }
                }
                else if (currentRootPage == PAGE.RECENT_ORDER)
                {
                    if (backHistoryRecentOrder.Count >= 2)
                    {
                        backHistoryRecentOrder.Pop();
                    }
                }
                else if (currentRootPage == PAGE.CURRENT_ORDER)
                {
                    if (backHistoryCurrentOrder.Count >= 2)
                    {
                        backHistoryCurrentOrder.Pop();
                    }
                }

                return;
            }

            if (page == PAGE.CURRENT_ORDER)
            {
                setCurrentRootPage(PAGE.CURRENT_ORDER);
            }

            //manage pages
            if (currentRootPage == PAGE.HOME)
            {
                if (page == PAGE.HOME)
                {
                    backHistoryHome.Clear();
                    showButton(BUTTON_VISIBILITY.INFO_RIGHT);
                }
                else
                {
                    showButton(BUTTON_VISIBILITY.BACK_RIGHT);

                    if (page == PAGE.ORDER_SCREEN)
                    {
                        (Pages[(int)PAGE.ORDER_SCREEN] as OrderScreen).DrinkName = Util.currentOrder.drink.DrinkName;
                        (Pages[(int)PAGE.ORDER_SCREEN] as OrderScreen).CategoryName = Util.currentOrder.category.Catergory;
                    }
                    else if (page == PAGE.MIXER_SCREEN)
                    {
                        (Pages[(int)PAGE.MIXER_SCREEN] as ViewMixerScreen).DrinkName = Util.currentOrder.drink.DrinkName;
                    }
                    else if (page == PAGE.HOW_MUCH_CURRENT)
                    {
                            (Pages[(int)PAGE.HOW_MUCH_CURRENT] as HowMuchScreen).Price = "0";
                    }
                }

                backHistoryHome.Push(page);
            }
            else if (currentRootPage == PAGE.RECENT_ORDER)
            {
                if (page == PAGE.RECENT_ORDER)
                {
                    ((RecentOrderScreen)Pages[(int)page]).loadAllRecentOrders();
                    backHistoryRecentOrder.Clear();
                    showButton(BUTTON_VISIBILITY.NONE_RIGHT);
                }
                else
                {
                    showButton(BUTTON_VISIBILITY.BACK_RIGHT);
                }

                backHistoryRecentOrder.Push(page);
            }
            else if (currentRootPage == PAGE.CURRENT_ORDER)
            {
                if (page == PAGE.CURRENT_ORDER)
                {
                    backHistoryCurrentOrder.Clear();
                    showButton(BUTTON_VISIBILITY.NONE_RIGHT);
                }
                else
                {
                    showButton(BUTTON_VISIBILITY.BACK_RIGHT);
                }

                backHistoryCurrentOrder.Push(page);
            }
            pageTransitionControl.ShowPage(Pages[(int)page]);
            setCurrentRootPage(page);
        }

        private void ChangeFullScreenNumpadPage(PAGE page)
        {
            if (page == PAGE.HOW_MUCH_CURRENT )
            {
                pageTransitionControl.Height += header.ActualHeight;
                pageTransitionControl.Margin = new Thickness(0, (-1) * header.ActualHeight, 0, 0);
            }
            else
            {
                pageTransitionControl.Height = 604;
                pageTransitionControl.Margin = new Thickness(0);
            }
        }

        public void ShowPrevPage()
        {
            if (currentRootPage == PAGE.HOME)
            {
                if (backHistoryHome.Count == 1)
                {
                    //At the root page
                    showButton(BUTTON_VISIBILITY.INFO_RIGHT);
                    pageTransitionControl.ShowPage(Pages[(int)PAGE.HOME]);
                }
                else
                {
                    //Remove current page
                    backHistoryHome.Pop();
                    //Get previous page
                    PAGE page = backHistoryHome.Peek();
                    //ChangeFullScreenNumpadPage(page);
                    if (page == PAGE.HOME)
                    {
                        showButton(BUTTON_VISIBILITY.INFO_RIGHT);
                    }
                    else
                    {
                        showButton(BUTTON_VISIBILITY.BACK_RIGHT);
                    }

                    pageTransitionControl.ShowPage(Pages[(int)page]);
                }
            }
            else if (currentRootPage == PAGE.RECENT_ORDER)
            {
                if (backHistoryRecentOrder.Count == 1)
                {
                    //At the root page
                    showButton(BUTTON_VISIBILITY.NONE_RIGHT);
                    pageTransitionControl.ShowPage(Pages[(int)PAGE.RECENT_ORDER]);
                }
                else
                {
                    //Remove current page
                    backHistoryRecentOrder.Pop();
                    //Get previous page
                    PAGE page = backHistoryRecentOrder.Peek();
                    if (page == PAGE.RECENT_ORDER)
                    {
                        showButton(BUTTON_VISIBILITY.NONE_RIGHT);
                    }
                    else
                    {
                        showButton(BUTTON_VISIBILITY.BACK_RIGHT);
                    }

                    pageTransitionControl.ShowPage(Pages[(int)page]);
                }
            }
            else if (currentRootPage == PAGE.CURRENT_ORDER)
            {
                if (backHistoryCurrentOrder.Count == 1)
                {
                    //At the root page
                    showButton(BUTTON_VISIBILITY.NONE_RIGHT);
                    pageTransitionControl.ShowPage(Pages[(int)PAGE.CURRENT_ORDER]);
                }
                else
                {
                    //Remove current page
                    backHistoryCurrentOrder.Pop();
                    //Get previous page
                    PAGE page = backHistoryCurrentOrder.Peek();
                    if (page == PAGE.CURRENT_ORDER)
                    {
                        showButton(BUTTON_VISIBILITY.NONE_RIGHT);
                    }
                    else
                    {
                        showButton(BUTTON_VISIBILITY.BACK_RIGHT);
                    }

                    pageTransitionControl.ShowPage(Pages[(int)page]);
                }
            }

        }
        #endregion

        #region Panel Button Handler
        #region Header
        private void btnRight_Click(object sender, RoutedEventArgs e)
        {
            setCurrentRootPage(PAGE.CURRENT_ORDER);
            ShowPage(PAGE.CURRENT_ORDER);
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            ShowPrevPage();
        }
    /*    protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if ( currentRootPage == PAGE.HOME && backHistoryHome.Count > 1)
            {
                this.ShowPrevPage();
                e.Cancel = true;
            }
            else if (currentRootPage == PAGE.CURRENT_ORDER && backHistoryCurrentOrder.Count > 1)
            {
                this.ShowPrevPage();
                e.Cancel = true;
            }
            else if (currentRootPage == PAGE.RECENT_ORDER && backHistoryRecentOrder.Count > 1)
            {
                this.ShowPrevPage();
                e.Cancel = true;
            }
            
        }*/
        private void btnInfo_Click(object sender, RoutedEventArgs e)
        {
            ShowPage(PAGE.INFO);
        }
        #endregion

        #region Footer
        private void btnHome_Click(object sender, RoutedEventArgs e)
        {
            setCurrentRootPage(PAGE.HOME);
            ShowPage(PAGE.HOME);
        }

        private void btnRecentOrder_Click(object sender, RoutedEventArgs e)
        {
            setCurrentRootPage(PAGE.RECENT_ORDER);
            ShowPage(PAGE.RECENT_ORDER);
        }

        private void btnCurrentOrder_Click(object sender, RoutedEventArgs e)
        {
            setCurrentRootPage(PAGE.CURRENT_ORDER);
            ShowPage(PAGE.CURRENT_ORDER);
        }
        #endregion
        #endregion

    }

    public enum PAGE
    {
        HOME = 0,
        INFO = 1,
        CHOOSE_CATEGORY = 2,
        CURRENT_ORDER = 3,
        RECENT_ORDER = 4,
        ORDER_ITEM_DETAIL = 5,
        SUMARY_SCREEN = 6,
        SEARCH_SCREEN = 7,
        CLASS_SIZE_SCREEN = 8,
        ORDER_SCREEN = 9,
        MIXER_SCREEN = 10,
        HOW_MUCH_CURRENT = 11
    }

    public enum BUTTON_VISIBILITY
    {
        BACK_RIGHT,
        INFO_RIGHT,
        NONE_RIGHT
    }
}